package User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Password extends JFrame {

    private String username;
    private JPanel contentPanel;
    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField;
    private JButton confirmButton;

    public Password(String username) {
        this.username = username;
        setTitle("密码管理");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 这里设置为关闭当前窗口，可按需调整
        setLocationRelativeTo(null);

        contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3, 2));

        JLabel oldPasswordLabel = new JLabel("旧密码：");
        oldPasswordField = new JPasswordField();
        JLabel newPasswordLabel = new JLabel("新密码：");
        newPasswordField = new JPasswordField();

        inputPanel.add(oldPasswordLabel);
        inputPanel.add(oldPasswordField);
        inputPanel.add(newPasswordLabel);
        inputPanel.add(newPasswordField);

        confirmButton = new JButton("确认修改");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                char[] oldPasswordChars = oldPasswordField.getPassword();
                char[] newPasswordChars = newPasswordField.getPassword();
                String oldPassword = new String(oldPasswordChars);
                String newPassword = new String(newPasswordChars);
                if (checkOldPassword(oldPassword)) {
                    updatePasswordInDatabase(newPassword);
                    JOptionPane.showMessageDialog(contentPanel, "密码修改成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(contentPanel, "旧密码错误，请重新输入！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        inputPanel.add(new JLabel()); // 占位，保持布局对齐
        inputPanel.add(confirmButton);

        contentPanel.add(inputPanel, BorderLayout.CENTER);

        setContentPane(contentPanel);
        setVisible(true);
    }

    private boolean checkOldPassword(String oldPassword) {
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookmanagement", "root", "root");
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT password FROM usercreate WHERE username =?")) {
            preparedStatement.setString(1, username);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    String storedPassword = resultSet.getString("password");
                    return storedPassword.equals(oldPassword);
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    private void updatePasswordInDatabase(String newPassword) {
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookmanagement", "root", "root");
             PreparedStatement preparedStatement = connection.prepareStatement("UPDATE usercreate SET password =? WHERE username =?")) {
            preparedStatement.setString(1, newPassword);
            preparedStatement.setString(2, username);
            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public JPanel getContentPanel() {
        return contentPanel;
    }


}